#include <QMainWindow>
#include <QFileDialog>
#include <QString>
#include <QPushButton>
#include <QMessageBox>

#include <kaboutdata.h>
#include <kcmdlineargs.h>

#include <stdio.h>

#include "screen.h"
#include "screen_var.h"
#include "calculator.h"
#include "menu_var.h"
#include "global.h"

extern "C" {
  //#include "calc.h"
}

int main(int argc, char *argv[])
{
  //QApplication app(argc, argv);
  //app.setQuitOnLastWindowClosed(false);
  KAboutData about("wabbitemu","WabbitEmu",ki18n("WabbitEmu"),"1.0",ki18n("TI calculator emulator"),KAboutData::License_GPL,ki18n("Copyright (C) 2008 Revolution Software. All rights reserved."),ki18n("If you are a TI-83 Plus programmer, try the Z80 Assembly IDE and SPASM too"),"http://www.revsoft.org/");
  KCmdLineArgs::init(argc,argv,&about);
  KApplication app;
  gApp=&app;
  macroRunning=0;
  gifCaptureRunning=0;
  mouseKey=0; // hopefully no keys are equated to 0
  zoomAmount=1;
  settings = new QSettings("RevSoft","WabbitEmu");

  gifout=fopen("gifout.gif", "wb");
  im=gdImageCreate(96,64);
 int black = gdImageColorAllocate(im, 0, 0, 0); 
 gdImageRectangle(im, 0, 0, 10, 10, black);

//fclose(gifout);
//gdImageDestroy(im);
  screenimpl = new screen;
  //app.connect( &app, SIGNAL( lastWindowClosed() ), screenimpl, SLOT( exitEmulator() ) );
  app.connect( &app, SIGNAL ( aboutToQuit () ), screenimpl, SLOT (exitEmulator() ) );
  app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );

  QMainWindow window;
  gWin=&window;

  if(settings->contains("options/drawSkin")){
    drawSkin=settings->value("options/drawSkin").toInt();
  }
  else{
    drawSkin=1;
  }
  if(settings->contains("options/zoom")){
    zoomAmount=settings->value("options/zoom").toDouble();
    if(!drawSkin)window.setGeometry(0,0,96*zoomAmount-1,64*zoomAmount-1);
    else window.setGeometry(0,0,315,689);
  }
  else{
    zoomAmount=1;
  }
  window.setWindowTitle("WabbitEmu");
  window.show();
//
  if(!settings->contains("options/rom")){
    fileName = QFileDialog::getOpenFileName(&window, "WabbitEmu: Please select a ROM","/","Known file types (*.rom *.sav)"); 
    if(fileName==""){
      delete screenimpl;
      return 1; // main Qt loop hasn't started yet
    }
     settings->setValue("options/rom",fileName);
  }

 else{
    QByteArray b_fileName = settings->value("options/rom").toByteArray();
    fileName = b_fileName;
 }


QByteArray ba_fileName = fileName.toLatin1();
char *c_fileName = ba_fileName.data(); 
  printf("FILENAME:%s",c_fileName);

  // TODO: change skins according to which calculator model
  skin=new QImage("ti-83plus.bmp");
  if(skin->isNull()){
    QMessageBox::warning ( &window, "Unable to Load TI-83 Plus Skin", "Unable to load TI-83 Plus skin - using skinless mode");
    // TODO: do stuff to make it work properly and be skinless, resize window
  }

  calculator* calculatorimpl = new calculator;
  window.setCentralWidget(calculatorimpl);
  calculatorimpl->update();
  calculatorimpl->show();

 // screenimpl->setGeometry(0,0,314,688);
  screenimpl->setContextMenuPolicy(Qt::CustomContextMenu);
  screenimpl->setParent(calculatorimpl);
  screenimpl->setFocusPolicy(Qt::StrongFocus);
  screenimpl->show();

  menu = new QMenu;
  QAction* Send_File = new QAction("Send File...",menu);
  Send_File->setShortcut(Qt::Key_F10);
  QAction* Sound = new QAction("Sound",menu);
  QAction* Enter_Debugger = new QAction("Enter Debugger...",menu);
  QAction* Load_State = new QAction("Load State...",&window);
  QAction* Save_State = new QAction("Save State...",menu);
  QAction* Reload_Last_State = new QAction("Reload Last State",menu);
  QAction* Reset_Calculator = new QAction("Reset Calculator",menu);
  QAction* Start_GIF_Capture = new QAction("Start/Stop GIF Capture",menu);
  Start_GIF_Capture->setShortcut(Qt::Key_Backspace);
  QAction* Copy_ANS_to_Clipboard = new QAction("Copy ANS to Clipboard",menu);
  QAction* Options = new QAction("Options...",menu);
  QAction* Visit_Website = new QAction("Visit Website",menu);
  QAction* Exit_And_Save_State = new QAction("Exit and Save State",menu);
  QAction* Exit_Without_Saving_State = new QAction("Exit Without Saving State",menu);
  menu->addAction(Send_File);
  window.addAction(Send_File); // So shortcut is recognized
  menu->addAction(Sound);
  menu->addSeparator();
  menu->addAction(Enter_Debugger);
  menu->addAction(Load_State);
  menu->addAction(Save_State);
  menu->addAction(Reload_Last_State);
  menu->addAction(Reset_Calculator);
  menu->addSeparator();
  menu->addAction(Start_GIF_Capture);
  window.addAction(Start_GIF_Capture);
  QMenu* Key_Input_Macros = menu->addMenu("Key Input Macros");
  QAction* Record = new QAction("Start/Stop Recording",Key_Input_Macros);
  //Record->setShortcut(Qt::Key_R | Qt::Key_CapsLock);
  QAction* Run = new QAction("Run",Key_Input_Macros);
  QAction* Delete = new QAction("Delete",Key_Input_Macros);
  QAction* View_Macros = new QAction("View Macros...",Key_Input_Macros);
  Key_Input_Macros->addAction(Record);
  Key_Input_Macros->addAction(Run);
  Key_Input_Macros->addAction(Delete);
  Key_Input_Macros->addAction(View_Macros);
  QMenu* Display=menu->addMenu("Display");
  QAction* Skinless = new QAction("Toggle Skinless",Display);
  QAction* CutOut = new QAction("Cut-Out VTI-Style",Display);
  QAction* Zoom1 = new QAction("Zoom 1x",Display);
  QAction* Zoom15 = new QAction("Zoom 1.5x",Display);
  QAction* Zoom2 = new QAction("Zoom 2x",Display);
  QAction* Zoom3 = new QAction("Zoom 3x",Display);
  Display->addAction(Skinless);
  Display->addAction(CutOut);
  Display->addAction(Zoom1);
  Display->addAction(Zoom15);
  Display->addAction(Zoom2);
  Display->addAction(Zoom3);
  menu->addSeparator();
  menu->addAction(Copy_ANS_to_Clipboard);
  menu->addSeparator();
  menu->addAction(Options);
  menu->addAction(Visit_Website);
  menu->addAction(Exit_And_Save_State);
  menu->addAction(Exit_Without_Saving_State);

  short counter;
  // 158,171,136 is a white pixel
  for(counter=255;counter>=0;--counter){
    QColor RGBColor((int)(158*counter/255),(int)(171*counter/255),(int)(136*counter/255));
    colorVector.append(RGBColor.rgb());
  }

  timer = new QTimer;
  timer->setSingleShot(false);

  //calc = calc_new();
  //calc_load(calc, c_fileName);

  printf("test");
  calc = calc_slot_new();
  calc = rom_load(calc, c_fileName);
  printf("\nslot:%d",calc);

  //app.connect( timer, SIGNAL( timeout() ), screenimpl, SLOT( update() ) );
/* // test code
  QMessageBox messagebox;
  messagebox.setText("hello");
*/
  app.connect (screenimpl, SIGNAL ( customContextMenuRequested(const QPoint & ) ), screenimpl, SLOT ( menuRequested(const QPoint &) ));

  app.connect( Send_File, SIGNAL ( triggered() ), screenimpl, SLOT ( sendFile() ));
  app.connect( Load_State, SIGNAL ( triggered() ), screenimpl, SLOT ( loadState() ));
  app.connect( Save_State, SIGNAL ( triggered() ), screenimpl, SLOT ( saveState() ));
  app.connect( Reset_Calculator, SIGNAL ( triggered() ), screenimpl, SLOT ( resetCalculator() ) );
  app.connect( Start_GIF_Capture, SIGNAL ( triggered() ), screenimpl, SLOT ( startGIFCapture() ) );
  app.connect( Record, SIGNAL ( triggered() ), screenimpl, SLOT ( recordKeyMacro() ));
  app.connect( Run, SIGNAL ( triggered() ), screenimpl, SLOT ( runKeyMacro() ));
  app.connect( Delete, SIGNAL ( triggered() ), screenimpl, SLOT ( deleteKeyMacro() ));
  app.connect( View_Macros, SIGNAL ( triggered() ), screenimpl, SLOT ( viewKeyMacros() ));
  app.connect( Skinless, SIGNAL ( triggered() ), calculatorimpl, SLOT ( skinless() ) );
  app.connect( CutOut, SIGNAL ( triggered() ), calculatorimpl, SLOT ( cutOut() ) );
  app.connect( Zoom1, SIGNAL ( triggered() ), calculatorimpl, SLOT ( zoom1() ) );
  app.connect( Zoom15, SIGNAL ( triggered() ), calculatorimpl, SLOT ( zoom15() ) );
  app.connect( Zoom2, SIGNAL ( triggered() ), calculatorimpl, SLOT ( zoom2() ) );
  app.connect( Zoom3, SIGNAL ( triggered() ), calculatorimpl, SLOT ( zoom3() ) );
  app.connect( Options, SIGNAL ( triggered() ), screenimpl, SLOT ( options() ) );
  app.connect( Visit_Website, SIGNAL ( triggered() ), screenimpl, SLOT ( visitWebsite() ));
  app.connect( Exit_Without_Saving_State, SIGNAL ( triggered() ), screenimpl, SLOT ( exitEmulator() ));

  app.connect( timer, SIGNAL( timeout() ), screenimpl, SLOT( update() ) );
  timer->start(20); // TODO: um...how much? // use correction timer thing like GTK version does
  return app.exec();

} 
